#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from models.sleep_monitoring import SleepMonitoringRecord
from config import get_database

def check_database():
    try:
        # 连接数据库
        db = get_database()
        db.connect()
        print('✅ 数据库连接成功')
        
        # 检查表是否存在
        table_exists = SleepMonitoringRecord.table_exists()
        print(f'📋 表是否存在: {table_exists}')
        
        if not table_exists:
            print('❌ sleep_monitoring_records 表不存在')
            return
        
        # 统计记录数量
        count = SleepMonitoringRecord.select().count()
        print(f'📊 记录总数: {count}')
        
        if count == 0:
            print('⚠️  数据库中没有任何记录')
            return
        
        # 显示最近的记录
        print('\n📝 最近5条记录:')
        records = list(SleepMonitoringRecord.select().order_by(SleepMonitoringRecord.created_at.desc()).limit(5))
        
        for i, r in enumerate(records, 1):
            print(f'{i}. 会话ID: {r.session_id[:8]}...')
            print(f'   房间ID: {r.room_id}')
            print(f'   开始时间: {r.start_time}')
            print(f'   结束时间: {r.end_time}')
            print(f'   总时长: {r.total_seconds}秒')
            print(f'   主要状态: {r.dominant_state}')
            print('---')
            
    except Exception as e:
        print(f'❌ 错误: {e}')
    finally:
        if 'db' in locals() and not db.is_closed():
            db.close()
            print('🔒 数据库连接已关闭')

if __name__ == '__main__':
    check_database()